﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using System.Xml;
using System.IO;
using System.Xml.Serialization;
using Tong.Serialize;
public partial class 序列化类型_XmlSerialize : System.Web.UI.Page
{
    SerializeXML MyTest = new SerializeXML();
    Tong.TongTreeDataTable TreeTable = new Tong.TongTreeDataTable();
    protected void Page_Load(object sender, EventArgs e)
    {


        DataTable Mydt = TreeTable.TreeTable("CX_Column",
            Tong.DataOperation.Data_CX_Column._CX_Column_id,
            Tong.DataOperation.Data_CX_Column._CX_Column_id,
            Tong.DataOperation.Data_CX_Column._CX_Column_Name,
            Tong.DataOperation.Data_CX_Column._CX_Column_followid
        );
        Mydt.TableName = "啊啊.";
        Label1.Text = Server.HtmlEncode(MyTest.SerializeClassToXML(Mydt
            )).Replace("\r\n", "<br/>").Replace("\t", " &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;");


        Mydt = (DataTable)MyTest.SerializeXMLToClass(
            Server.HtmlDecode(Label1.Text.Trim().Replace("<br/>", "\r\n").Replace(" &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;", "\t")
            )
            , typeof(DataTable));

        StringWriter Sw = new StringWriter();
        Html32TextWriter HT = new Html32TextWriter(Sw);
        DataGrid MyGrid = new DataGrid();
        MyGrid.DataSource = Mydt;
        MyGrid.DataBind();
        MyGrid.RenderControl(HT);
        Label1.Text = Sw.ToString();

       

        //Response.Write(Sw.ToString());


          
    }






}

namespace Tong.Serialize
{

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;

    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;


    using System.Xml;
    using System.IO;
    using System.Xml.Serialization;
    using Tong.Serialize;

    //加入序列化類型
    ///二進制序列化
    using System.Runtime.Serialization.Formatters.Binary;


    /// <summary>
    /// XML序列化类型
    /// </summary>
    public class SerializeXML
    {

        /// <summary>
        /// 用于序列化的 write 
        /// </summary>
        private System.IO.StringWriter Write = new System.IO.StringWriter();

        /// <summary>
        /// 开始定义用于序列化的新实例
        /// </summary>
        private System.Xml.Serialization.XmlSerializer MySerializeXml;

        /// <summary>
        /// 用於資料讀出的資料實例
        /// </summary>
        private System.IO.StringReader SR;


        /// <summary>
        /// 使用XML序列化
        /// </summary>
        /// <param name="type">要傳入的類型的實例</param>
        /// <returns></returns>
        public string SerializeClassToXML(object type)
        {
            MySerializeXml = new System.Xml.Serialization.XmlSerializer(type.GetType()); ;
            MySerializeXml.Serialize(Write, type);
            return Write.ToString();
        }

        /// <summary>
        /// 類型反序列化
        /// </summary>
        /// <param name="XMLstring">類型序列化後的XML字符串</param>
        /// <param name="type">傳入當前類型的類型</param>
        /// <returns></returns>
        public object SerializeXMLToClass(string XMLstring, Type type)
        {
            SR = new System.IO.StringReader(XMLstring);
            MySerializeXml = new System.Xml.Serialization.XmlSerializer(type);
            return MySerializeXml.Deserialize(SR);

        }
    }

    /// <summary>
    /// 以SOAP可格式對對象 序列化的類
    /// </summary>
    public class SerializeSOAP
    {
        /// <summary>
        /// 流對象用於對資料的返回
        /// </summary>
        System.IO.MemoryStream StreamMemory;

        /// <summary>
        /// 用SOAP序列化對像的實例操作方法
        /// </summary>
        System.Runtime.Serialization.Formatters.Soap.SoapFormatter MySerializeSOAP;

        /// <summary>
        /// 傳入一個對像把它返回成 一個記憶體的流
        /// </summary>
        /// <param name="objectModel">序列化的對像</param>
        /// <returns></returns>
        public System.IO.MemoryStream SerializeClassToStream(object objectModel)
        {
            //實例化操作的各對象
            StreamMemory = new MemoryStream();
            MySerializeSOAP = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
            //開始執行操作
            MySerializeSOAP.Serialize(StreamMemory, objectModel);

            return StreamMemory;

        }


        /// <summary>
        ///  傳入一個記憶體的流把它返回成 一個對像
        /// </summary>
        /// <param name="Streamobj">返序列化的流</param>
        /// <returns></returns>
        public object SerializeStreamToClass(MemoryStream Streamobj)
        {
            //用於返回反序列華的類型的結果
            object Serializeobject;
            //實例化操作各對的對像
            MySerializeSOAP = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();

            Serializeobject = MySerializeSOAP.Deserialize(StreamMemory);

            StreamMemory.Close();//關了
            StreamMemory.Dispose();//清理

            return Serializeobject;
        }
    }

    /// <summary>
    /// 序列化类型
    /// </summary>
    public class SerializeBinary
    {

    }


}
public class test {
    string pp;
    public void oo() { }
    public string Pp
    {
        get { return pp; }
        set { pp = value; }
    }
}